package com.view.cn.mvprxjava.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Zengxiaoping on 2016/11/29.
 */

public class PathView extends View {

	private Paint mPaint;
	private int mWidth, mHeigt;

	public PathView(Context context) {
		super(context);
	}

	public PathView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initPaint();
	}

	private void initPaint() {
		mPaint = new Paint();                   // 创建画笔
		mPaint.setColor(Color.BLACK);           // 画笔颜色 - 黑色
		mPaint.setStyle(Paint.Style.STROKE);    // 填充模式 - 描边
		mPaint.setStrokeWidth(10);              // 边框宽度 - 10
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		//lineTo
		canvas.translate(mWidth / 8, mHeigt / 8);
		Path path = new Path();
		path.lineTo(100, 100);
		path.lineTo(100, 0);
		path.close();//close方法用于连接当前最后一个点和最初的一个点(如果两个点不重合的话)，最终形成一个封闭的图形。
		canvas.drawPath(path, mPaint);

		/**
		 * 方法名	            简介	                是否影响之前的操作	    是否影响之后操作
		 * moveTo	    移动下一次操作的起点位置	            否	                是
		 * setLastPoint	设置之前操作的最后一个点位置	        是	                是
		 */
		//moveTo
		canvas.translate(mWidth / 8, mHeigt / 8);
		Path path1 = new Path();
		path1.lineTo(100, 100);
		path1.moveTo(100, 50);
		path1.lineTo(100, 0);
		canvas.drawPath(path1, mPaint);
		//setLastPosition
		canvas.translate(mWidth / 8, mHeigt / 8);
		Path path2 = new Path();
		path2.lineTo(100, 100);
		path2.setLastPoint(100, 50);
		path2.lineTo(100, 0);
		canvas.drawPath(path2, mPaint);

		//画矩形，注意有方向
		canvas.translate(mWidth / 8, mHeigt / 8);
		Path path3 = new Path();
		path3.addRect(-50, -50, 50, 50, Path.Direction.CW);
		path3.setLastPoint(-100,100);
		canvas.drawPath(path3, mPaint);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mWidth = w;
		mHeigt = h;
	}
}
